﻿using System;
using System.Runtime.CompilerServices;
using Microsoft.Scripting.Runtime;

namespace Cjc.Calculation.ExpressionEngine.Excel.Runtime.Operations
{
	public static class StringOps
	{
		[ImplicitConversionMethod]
		public static Int32 ConvertToInt32( string x ) { return int.Parse( Default( x ) ); }
		[ImplicitConversionMethod]
		public static Int64 ConvertToInt64( string x ) { return long.Parse( Default( x ) ); }
		[ImplicitConversionMethod]
		public static double ConvertToDouble( string x ) { return double.Parse( Default( x ) ); }
		[ImplicitConversionMethod]
		public static decimal ConvertToDecimal( string x ) { return decimal.Parse( Default( x ) ); }

		[SpecialName]
		public static object Add( string x, string y ) { return x + y; }

		private static string Default( string s )
		{
			if ( s != null ) s = s.Trim();

			return string.IsNullOrEmpty( s ) ? "0" : s;
		}
	}
}